package com.example.forum.utils;

import com.example.forum.common.ResultCode;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.User;
import org.springframework.util.StringUtils;

/**
 * 功能描述:
 *
 * @author Lenovo
 * @date 2024/12/12
 */
public class SetUserUtil {
    public static void setUser(User user, String username, String nickname, String password) {
        if (user == null || !StringUtils.hasLength(username)
                || !StringUtils.hasLength(nickname)
                || !StringUtils.hasLength(password)){
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE.toString());
        }
            user.setUsername(username);
        user.setNickname(nickname);
        String salt = UUIDUtil.getSalt();
        String encryptPassword = MD5Util.encrypt(password, salt);
        user.setPassword(encryptPassword);
        user.setSalt(salt);
    }
}
